package com.dang.alex.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.dang.alex.dao.LoginDAO;
import com.dang.alex.model.User;
import com.dang.alex.util.DBUtils;

public class LoginDAOImp implements LoginDAO{
	
	String translate_sql="select * from d_user where email=? and password=?";
	@Override
	public User translate(String email,String password) throws Exception {
			Connection conn=null;
			PreparedStatement ps=null;
			ResultSet rs= null;
			User user= null;
			try {
				conn=DBUtils.getTnstace().getConnection();
				ps=conn.prepareStatement(translate_sql);
				ps.setString(1, email);
				ps.setString(2, password);
				rs=ps.executeQuery();
				
				if(rs.next()) {
					user= new User();
					user.setId(rs.getInt("id"));
					user.setEmail(rs.getString("email"));
					user.setNickname(rs.getString("nickname"));;
					user.setPassword(rs.getString("password"));
					user.setUserIntegral(rs.getInt("user_integral"));
					if(rs.getString("is_email_verify")=="T") {
						user.setEmailVerify(true);
					}else {
						user.setEmailVerify(false);
					}
					user.setEmailVerifyCode(rs.getString("email_verify_code"));
					user.setLastLoginTime(rs.getLong("last_login_time"));
					user.setLastLoginIp(rs.getString("last_login_ip"));
					
				}
				
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			DBUtils.getTnstace().close(conn);
			DBUtils.getTnstace().close(ps);
			DBUtils.getTnstace().close(rs);
		}
		return user;
	}

}
